package com.kischang.blog.web.action;

import com.kischang.blog.model.PageInfo;
import com.kischang.blog.model.TitleMenu;
import com.kischang.blog.model.Topic;
import com.kischang.blog.service.TitleMenuService;
import com.kischang.blog.service.TopicService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@Scope("prototype")
public class IndexAction extends ActionSupport implements ModelDriven<PageInfo>{

	private static final long serialVersionUID = 3701416667851199108L;
	
	@Autowired
	private TitleMenuService titleMenuService;
	@Autowired
	private TopicService topicService;
	
	private List<TitleMenu> menulist;
	private List<Topic> topiclist;
	private int mid;

	private PageInfo pageInfo = new PageInfo(10);

	@Override
	public String execute() throws Exception {
		menulist = titleMenuService.getAllParentMenu();
		topiclist = topicService.getTopicByPage(pageInfo);
		return SUCCESS;
	}

	public String view(){
		menulist = titleMenuService.getAllParentMenu();
		if(mid > 0){
            topiclist = topicService.getTopicByPageAndMenu(mid, pageInfo);
            pageInfo.setUrl("?mid="+mid);
        }
		else{
            topiclist = topicService.getTopicByPage(pageInfo);
            pageInfo.setUrl("");
        }
		return SUCCESS;
	}
	
	public List<TitleMenu> getMenulist() {
		return menulist;
	}

	public List<Topic> getTopiclist() {
		return topiclist;
	}
	public int getMid() {
		return mid;
	}
	public void setMid(int mid) {
		this.mid = mid;
	}
	@Override
	public PageInfo getModel() {
		return pageInfo;
	}
}
